﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class ANALYZEBATCH : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public INTEXDataSet setUI
        {
            get
            {
                return (INTEXDataSet)TryFindResource("iNTEXDataSet");
            }
        }
        ANALYZEBATCHData m_Data, m_DataX;
        public DataSet setResult;
        
        private DataRow dwUI;
        ANALYZEBATCHReport m_rpt;
        bool flagRegenerateLastMonthData;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        DataSet setANINFOR = new DataSet(); 
        DataTable tableUI;
        private string frominspectiondate = "", endinspectiondate = "";
        string LASTINVDATE;
        public ANALYZEBATCH()
        {
            InitializeComponent();
            this.Name = "完缸分析表";
            this.Title = "完缸分析表";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                InitializeComponent();
               
                this.dicParamater = dicParamater;
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
                m_Data = new ANALYZEBATCHData();
                m_Data.OracleDB = this.OracleDB;
                m_Data.proxy = this.proxy;
                tableUI = new DataTable("TableUI");
                tableUI.Columns.Add("FROMDATE", typeof(string));
                tableUI.Columns.Add("ENDDATE", typeof(string));
                tableUI.Columns.Add("LOT", typeof(string));
                dwUI = tableUI.NewRow();
                this.setUI.Tables.Clear(); 
                flagRegenerateLastMonthData = false; 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        #endregion

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                finishedcount = 0;
                setUI.Clear();
                setUI.AcceptChanges();

                dwUI["FROMDATE"] = frominspectiondate;
                dwUI["ENDDATE"] = endinspectiondate;
                dwUI["LOT"] = LOT.Text.ToString().Trim();
                
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    setResult = m_Data.BuildGBBEW(dwUI);
                    
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["ANALYZEBATCH"];
                   
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dateChooser = (DatePicker)this.FindName("DateChooser");
                dwUI = tableUI.NewRow();                
                if (setUI != null)
                {
                   // setResult = m_Data.GetWASTAMM();
                   // setUI.Merge(setResult);
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        } 
        public void ItemsControls_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;  
                     
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
       
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void GenerateReport(string PrintType)
        {
            Process[] AllProcesses = Process.GetProcessesByName("EXCEL");

            // check to kill the right process
            /*foreach (Process ExcelProcess in AllProcesses)
            {
                ExcelProcess.Kill();
            }*/
            using (new LongOperation(this, "读取符合条件的记录"))
            { 
                m_DataX = new ANALYZEBATCHData(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                m_rpt = new ANALYZEBATCHReport(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.PrintType = PrintType;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.WriteExcelReport("EXCEL");
            }
        }
        void txt_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;
            //tb.Text = "";
        }
        private void FROM_INSPECTION_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                frominspectiondate = "";
                frominspectiondate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_INSPECTION_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                endinspectiondate = "";
                endinspectiondate = m_Data.GetDate(sender);
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRegenerateLastInv_Click(object sender, RoutedEventArgs e)
        {
            flagRegenerateLastMonthData = true;
            btnQuery_Click(null, null);
            flagRegenerateLastMonthData = false;
        }

    }
}
